<@override name="head">
<title>两步验证设置</title>
<style type="text/css">
    #root {
        /*width: 800px !important;*/
        /*max-width: 70% !important;*/
    }

    .streams-tags {
        /*padding-top: 30px !important;*/
        /*padding-left: 50px !important;*/
        /*padding-right: 50px !important;*/
        /*padding-bottom: 30px !important;*/
        padding: 30px !important;
        text-align: center;
    }
</style>
</@override>

<@override name="contents">
<main class="lyear-layout-content">

    <div class="container-fluid">

        <div class="row">

            <div class="col-lg-12">
                <div class="card">
                    <header class="card-header">
                        <div class="card-title">2步骤验证向导</div>
                    </header>
                    <div class="card-body">

                        <form action="#!" class="lyear-guide-box" data-navigateable="true">
                            <ul class="nav nav-step">
                                <li class="nav-item">
                                    <span>2步骤验证提示信息</span>
                                    <a class="nav-link active" data-toggle="tab" href="#step-1"></a>
                                </li>

                                <li class="nav-item">
                                    <span>确认您的电子邮件地址</span>
                                    <a class="nav-link" data-toggle="tab" href="#step-2"></a>
                                </li>

                                <li class="nav-item">
                                    <span>安装和配置验证器应用程序</span>
                                    <a class="nav-link" data-toggle="tab" href="#step-3"></a>
                                </li>

                                <li class="nav-item">
                                    <span>确认2步骤验证设置</span>
                                    <a class="nav-link" data-toggle="tab" href="#step-4"></a>
                                </li>
                            </ul>
                            <!--对应内容-->
                            <div class="nav-step-content">
                                <div class="nav-step-pane d-none active" id="step-1" data-provide="validation">
                                    <!--                                    <div class="form-group">-->
                                    <!--                                        <label>姓名</label>-->
                                    <!--                                        <input class="form-control" type="text" id="username" required/>-->
                                    <!--                                    </div>-->
                                    2步骤验证为您的账户提供了一层额外的保护。一旦2步骤验证设置后，您除了需要一次性验证代码以后，还需要密码以登陆到系统。
                                    <b>请注意，您将需要移动设备来生成验证代码。</b>
                                </div>

                                <div class="nav-step-pane d-none" id="step-2" data-provide="validation">
                                    <div class="form-group">
                                        <p>请输入电子邮件地址。如果您的移动设备丢失，可以将紧急验证码发送到在此提供的电子邮件地址。</p>
                                        <label>邮箱地址</label>
                                        <input class="form-control" type="text" id="email" required/>
                                    </div>
                                </div>

                                <div class="nav-step-pane d-none" id="step-3" data-provide="validation">
                                    <div class="form-group">
                                        <p>请在移动设备商安装验证器应用程序。本系统支持以下验证器应用程序：Goole Authenticator(Android , IOS,
                                            BlackBerry),Authenticator
                                            (Windows Phone)</p>
                                        <p>通过扫描以下QR代码，打开和配置验证器应用程序。</p>
                                        <!--                                        <img src="https://blog.kdyzm.cn/blog/public/2025/06/16/dec25a2c00e74311befabe383620ca08.png" alt="QR代码">-->
                                        <img src="${codeImage}" alt="QR代码">
                                        <br/>
                                        <p>若因相机问题等无法扫描，可以手动输入秘钥：</p>
                                        <p>
                                            账号：${userName}<br/>
                                            秘钥：${key}
                                        </p>

                                    </div>
                                </div>

                                <div class="nav-step-pane d-none" id="step-4">
                                    <div class="form-group">
                                        <p>输入验证器应用程序生成的验证码，以确保配置正确</p>
                                        <label>6位数验证代码：</label>
                                        <input class="form-control" type="text" id="code" required/>
                                    </div>
                                </div>
                            </div>
                            <!--End 对应内容-->
                            <hr>
                            <div class="d-flex justify-content-between">
                                <button class="btn btn-secondary disabled" data-wizard="prev" type="button">上一步</button>
                                <button class="btn btn-secondary" data-wizard="next" type="button">下一步</button>
                                <button class="btn btn-primary d-none" data-wizard="finish">完成</button>
                            </div>
                        </form>

                    </div>
                </div>
            </div>

        </div>

    </div>

</main>
</@override>


<@override name="footer">
<script type="text/javascript" src="js/jquery.bootstrap.wizard.min.js"></script>
<script type="text/javascript" src="js/bootstrap-notify.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var guideObj = $('.lyear-guide-box');
        var nav_item = guideObj.find('.nav-item');
        var tab_pane = guideObj.find('.nav-step-pane');

        $('.lyear-guide-box').on('submit', function (e) {
            e.preventDefault();
            return false;
        });

        guideObj.bootstrapWizard({
            'tabClass': 'nav-step',
            'nextSelector': '[data-wizard="next"]',
            'previousSelector': '[data-wizard="prev"]',
            'finishSelector': '[data-wizard="finish"]',
            'onTabClick': function (e, t, i) {
                // data-navigateable='true'允许用户直接点击步骤项，false则只能通过按钮
                if (!$('.lyear-guide-box').is('[data-navigateable="true"]')) {
                    return false;
                }
            },
            'onTabShow': function (e, t, i) {
                t.children(":gt(" + i + ").complete").removeClass("complete");
                t.children(":lt(" + i + "):not(.complete)").addClass("complete");
            },
            'onNext': function (tab, navigation, index) {
                var current_index = guideObj.bootstrapWizard('currentIndex');
                var curr_tab = tab_pane.eq(current_index);

                var validator_selector = '[data-provide="validation"]';
                var validator = curr_tab.find(validator_selector).addBack(validator_selector);
                if (validator.length) {
                    var forms = validator.find('input, select, textarea');
                    var validation = true;
                    forms.each(function () {
                        if (this.checkValidity() === false) {
                            guideObj.addClass('was-validated');
                            validation = false;
                        } else {
                            guideObj.removeClass('was-validated');
                        }
                    });
                    if (!validation) {
                        guideObj.addClass('was-validated');
                        return false;
                    }
                }
            },
            'onPrevious': function () {
            },
            'onFinish': function (e, t, i) {
                var nav = nav_item.eq(i);
                nav.addClass('complete').find('a.nav-link').removeClass('active');
                // return false; // 这里为了演示效果，阻止其继续
                // 收集表单数据
                var formData = {
                    email: $('#email').val(),
                    code: $('#code').val()
                };

                // 发送AJAX请求
                $.ajax({
                    url: '/2fa/2fa_settings',
                    type: 'POST',
                    data: formData,
                    success: function (res) {
                        // 处理成功响应
                        if (!res.success || !res.data) {
                            $.notify({
                                message: '动态验证码错误，请确认动态验证码正确性或校对移动端时钟后重试',
                            }, {
                                type: 'danger',
                                placement: {
                                    from: 'top',
                                    align: 'right'
                                },
                                z_index: 10800,
                                delay: 1500,
                                animate: {
                                    enter: 'animate__animated animate__fadeInUp',
                                    exit: 'animate__animated animate__fadeOutDown'
                                }
                            });
                            return false;
                        }
                        $.notify({
                            message: '两步骤验证开启成功',
                        }, {
                            type: 'success',
                            placement: {
                                from: 'top',
                                align: 'right'
                            },
                            z_index: 10800,
                            delay: 1500,
                            animate: {
                                enter: 'animate__animated animate__fadeInUp',
                                exit: 'animate__animated animate__fadeOutDown'
                            }
                        });
                        setTimeout(function () {
                            location.href = '/index.html';
                        }, 1500);
                        return false;
                    },
                    error: function (xhr) {
                        // 处理错误
                        alert('设置失败: ' + xhr.responseText);
                    }
                });

                return false; // 阻止默认提交行为
            }
        });
    });
</script>
</@override>
<@extends name="layout/base_layout.html"/>

